package ymy.common;

import java.util.ArrayList;
import java.util.List;

public class Vehicle implements Comparable<Vehicle> {
    // 算法中间值
    public double algoValue = 0.0;

    // 车辆基本信息
    private int id;
    private Coordinate position;
    private int direction;
    private Area area;

    // 是否处于停驻状态
    private boolean parked;
    // 是否处于运行状态==>只针对活动车辆！！！
    private boolean running;

    // 中继处理时间
    private double delay;
    // 车辆行驶速度
    private double speed;
    // 车辆转发信号强度增益
    private double gain;

    // 移动距离
    private double distance;
    // 激活时间
    private double moment;

    // 车辆行驶路线
    private List<RoutePoint> driveRoutePath;

    // 相邻中继虚拟链路集合
    private List<VirtualLink> connectedVirtualLinks;
    // 选中中继虚拟链路集合
    private List<VirtualLink> selectedVirtualLinks;

    public Vehicle() {
        this.driveRoutePath = new ArrayList<>();
        this.connectedVirtualLinks = new ArrayList<>();
        this.selectedVirtualLinks = new ArrayList<>();
    }

    public void appendRoutePoint(RoutePoint routePoint) {
        this.driveRoutePath.add(routePoint);
    }

    public void clearVehicleStage() {
        this.algoValue = 0.0;
        this.connectedVirtualLinks.clear();
        this.selectedVirtualLinks.clear();
    }

    public void addConnectedVirtualLink(VirtualLink virtualLink) {
        this.connectedVirtualLinks.add(virtualLink);
    }

    public VirtualLink fetchVehicleConnectedVirtualLink(Vehicle v) {
        for (VirtualLink vl : this.connectedVirtualLinks) {
            if (vl.getEndv().equals(v))
                return vl;
        }
        return null;
    }

    public void addSelectedVirtualLink(VirtualLink virtualLink) {
        this.selectedVirtualLinks.add(virtualLink);
    }

    public VirtualLink fetchVehicleSelectedVirtualLink(Vehicle v) {
        for (VirtualLink vl : this.selectedVirtualLinks) {
            if (vl.getEndv().equals(v))
                return vl;
        }
        return null;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Coordinate getPosition() {
        return position;
    }

    public void setPosition(Coordinate position) {
        this.position = position;
    }

    public int getDirection() {
        return direction;
    }

    public void setDirection(int direction) {
        this.direction = direction;
    }

    public Area getArea() {
        return area;
    }

    public void setArea(Area area) {
        this.area = area;
    }

    public boolean isParked() {
        return parked;
    }

    public void setParked(boolean parked) {
        this.parked = parked;
    }

    public boolean isRunning() {
        return running;
    }

    public void setRunning(boolean running) {
        this.running = running;
    }

    public double getDelay() {
        return delay;
    }

    public void setDelay(double delay) {
        this.delay = delay;
    }

    public double getSpeed() {
        return speed;
    }

    public void setSpeed(double speed) {
        this.speed = speed;
    }

    public double getGain() {
        return gain;
    }

    public void setGain(double gain) {
        this.gain = gain;
    }

    public double getDistance() {
        return distance;
    }

    public void setDistance(double distance) {
        this.distance = distance;
    }

    public double getMoment() {
        return moment;
    }

    public void setMoment(double moment) {
        this.moment = moment;
    }

    public List<RoutePoint> getDriveRoutePath() {
        return driveRoutePath;
    }

    public void setDriveRoutePath(List<RoutePoint> driveRoutePath) {
        this.driveRoutePath = driveRoutePath;
    }

    public List<VirtualLink> getConnectedVirtualLinks() {
        return connectedVirtualLinks;
    }

    public void setConnectedVirtualLinks(List<VirtualLink> connectedVirtualLinks) {
        this.connectedVirtualLinks = connectedVirtualLinks;
    }

    public List<VirtualLink> getSelectedVirtualLinks() {
        return selectedVirtualLinks;
    }

    public void setSelectedVirtualLinks(List<VirtualLink> selectedVirtualLinks) {
        this.selectedVirtualLinks = selectedVirtualLinks;
    }

    @Override
    public boolean equals(Object o) {
        if (o == null) return false;
        if (o == this) return true;
        if (getClass() != o.getClass()) return false;
        Vehicle v = (Vehicle)o;
        return (this.id == v.id);
    }

    @Override
    public int compareTo(Vehicle o) {
        if (this.algoValue < o.algoValue)
            return -1;
        else if (this.algoValue > o.algoValue)
            return 1;

        return 0;
    }

    @Override
    public String toString() {
        String str = "Vehicle Information = {";
        str += "\n  ID = " + this.id;
        str += "\n  Location = " + this.position;
        str += "\n  Direction = " + Utility.stringDirection(this.direction);
        str += "\n  Status = " + ((this.parked) ? "PARKED" : "MOVED");
        str += "\n  Area = " + (this.area == null ? "null" : this.area);
        str += "\n  Speed = " + this.speed + "m/s";
        str += "\n}";
        return str;
    }
}